package com.tianqu.tms.www.financia.action;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.ibm.icu.text.SimpleDateFormat;
import com.tianqu.tms.core.base.Logger;
import com.tianqu.tms.core.base.TmsAction;
import com.tianqu.tms.core.base.UserContext;
import com.tianqu.tms.core.util.StringUtil;
import com.tianqu.tms.core.util.TmsConstants;
import com.tianqu.tms.dto.MarketCarInfoDto;
import com.tianqu.tms.dto.TmsMarketCarFinancialDto;
import com.tianqu.tms.dto.TmsMarketCarFinancialParam;
import com.tianqu.tms.entity.Car;
import com.tianqu.tms.entity.TmsFinancial;
import com.tianqu.tms.www.financia.controller.FinanciaCarCreationResultController;
import com.tianqu.tms.www.financia.viewhelper.FinanciaCarInputViewHelper;

public class FinanciaCarCreationResultAction extends TmsAction {

    private Logger logger = Logger.getLogger(FinanciaCarCreationResultAction.class);
	
	private static final String SYSPATH = "sysPath";
	
	private static final String LEFTLINE ="/";
	
	private static final String TMSPATH = "/tmsweb";
	private static final String BACKFLAG = "backFlag";
	@Override
	protected ActionForward doAction(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response,
			UserContext uc) throws Exception {
		logger.debug(this.getClass().getName() + "#execute was called.");
		
		FinanciaCarInputViewHelper viewhelp = (FinanciaCarInputViewHelper)uc.getAttribute(TmsConstants.VIEWHELP);
		request.setAttribute(TmsConstants.VIEWHELP, viewhelp);
		 
		String editId = (String)uc.getParameter("id");
		
		if(StringUtil.isNullOrBlank(viewhelp.getStartDate())) {
			String[] param = {"监管开始日期"};
			setErrorsToRequest(request, TmsConstants.U0005, param);
			uc.setAttribute(TmsConstants.VIEWHELP, viewhelp);
			request.setAttribute(TmsConstants.VIEWHELP, uc.getAttribute(TmsConstants.VIEWHELP));
			return mapping.getInputForward();
		}
		
		if(StringUtil.isNullOrBlank(viewhelp.getEndDate())) {
			String[] param = {"监管结束日期"};
			setErrorsToRequest(request, TmsConstants.U0005, param);
			uc.setAttribute(TmsConstants.VIEWHELP, viewhelp);
			request.setAttribute(TmsConstants.VIEWHELP, uc.getAttribute(TmsConstants.VIEWHELP));
			return mapping.getInputForward();
		}
		
		if ((!StringUtil.isNullOrBlank(viewhelp.getStartDate()))&&(!StringUtil.isNullOrBlank(viewhelp.getEndDate()))) {	
			SimpleDateFormat formatDate = new SimpleDateFormat("yyyy-MM-dd");
			//合同开始日期前段
			Date startDate = formatDate.parse(viewhelp.getStartDate());
			//合同开始日期后端				
			Date endDate = formatDate.parse(viewhelp.getEndDate());
			// 比较
			if (endDate.compareTo(startDate) < 0) {
				String[] param = {"监管开始日期"};
				setErrorsToRequest(request, TmsConstants.U0038, param);
				uc.setAttribute("errFlg", "true");
				request.setAttribute(TmsConstants.VIEWHELP, uc.getAttribute(TmsConstants.VIEWHELP));
				return mapping.getInputForward();
			}
		}
		
		if(viewhelp.getAttestStatus() <= 0) {
			String[] param = {"检测状态"};
			setErrorsToRequest(request, TmsConstants.U0005, param);
			uc.setAttribute(TmsConstants.VIEWHELP, viewhelp);
			request.setAttribute(TmsConstants.VIEWHELP, uc.getAttribute(TmsConstants.VIEWHELP));
			return mapping.getInputForward();
		}
		 
		List<MarketCarInfoDto> cars = Car.getInstance().getMarCarInfo(viewhelp.getMemberId(), viewhelp.getVin());
		if(cars.size() <= 0) {
			String[] param = {};
			setErrorsToRequest(request, TmsConstants.U9010, param);
			uc.setAttribute(TmsConstants.VIEWHELP, viewhelp);
			request.setAttribute(TmsConstants.VIEWHELP, uc.getAttribute(TmsConstants.VIEWHELP));
			return mapping.getInputForward();
		}
		
		TmsFinancial financial = TmsFinancial.getTmsFinancialPageInstance();
		if(StringUtil.isNotBlank(editId)) {
			TmsMarketCarFinancialDto dto = financial.getTmsMarketCarFinancial(editId);
			TmsMarketCarFinancialParam param = new TmsMarketCarFinancialParam();
			param.setVin(viewhelp.getVin());
			if(dto!=null && !dto.getVin().equals(viewhelp.getVin()) && financial.findTmsMarketCarFinancial(param).size()>0 ) {
				setErrorsToRequest(request, TmsConstants.U12004, null);
				uc.setAttribute(TmsConstants.VIEWHELP, viewhelp);
				request.setAttribute(TmsConstants.VIEWHELP, uc.getAttribute(TmsConstants.VIEWHELP));
				return mapping.getInputForward();
			}
		}else {//add
			TmsMarketCarFinancialParam param = new TmsMarketCarFinancialParam();
			param.setVin(viewhelp.getVin());
			if(financial.findTmsMarketCarFinancial(param).size()>0) {
				setErrorsToRequest(request, TmsConstants.U12004, null);
				uc.setAttribute(TmsConstants.VIEWHELP, viewhelp);
				request.setAttribute(TmsConstants.VIEWHELP, uc.getAttribute(TmsConstants.VIEWHELP));
				return mapping.getInputForward();
			}
		}
		
		FinanciaCarCreationResultController controller = new FinanciaCarCreationResultController();
		uc.setAttribute(SYSPATH, request.getSession().getServletContext().getRealPath(LEFTLINE).replace(TMSPATH, TmsConstants.BLANK));
		controller.execute(uc);
		
		FinanciaCarInputViewHelper view = new FinanciaCarInputViewHelper();
		view.setBackFlag("1");
		request.setAttribute(TmsConstants.VIEWHELP, view);
		logger.debug(this.getClass().getName() + "#execute was completed.");
		return mapping.findForward(TmsConstants.SUCCESS); 
	}
}
